<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="litchi" />&nbsp;<span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域:插槽-使用组件上的数据 -->
          <!-- #default="{data}" 接受组件上的数据data-指的是list中的一个一个的对象 -->
          <!-- <template #default="{data}"> -->
          <template slot-scope="{data}">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px ;width:100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="litchi" />&nbsp;<span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDelete(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :visible.sync="showDialog" :title="title" @close="hCloseDialog">
        <departDialog ref="departRef" :origin-list="originList" :cur-id="curId" :is-edit="isEdit" @success="hSuccess" @doClose="hClose" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, deleteDepartment } from '@/api/departments'
import { tranListToTreeList } from '@/utils/index'
import departDialog from './departDialog.vue'
export default {
  components: {
    departDialog
  },
  data() {
    return {
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false, // false是新增 \ true 是修改
      originList: []
    }
  },
  computed: {
    title() {
      return this.isEdit ? '编辑部门' : '添加部门'
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        const { data: res } = await getDepartments()
        // console.log(res, '部门信息')
        this.list = tranListToTreeList(res.depts)
        // 对初始数据进行处理
        this.originList = res.depts.map(({ code, id, pid, name }) => { return { code, id, pid, name } })
      } catch (error) {
        console.log(error, '获取部门信息错误')
      }
    },
    // 重置
    hCloseDialog() {
      // console.log('222')
      this.$refs.departRef.resetForm()
    },
    // 新增
    hAdd(id) {
      this.showDialog = true
      this.isEdit = false
      this.curId = id
    },
    // 修改
    hEdit(id) {
      this.showDialog = true
      this.isEdit = true
      this.curId = id
      // 引入departDialog组件的函数变化，解决编辑表单数据不更新的问题
      // 用settimeout是为了转成宏任务，先完成dom的更新，再执行里面的函数
      setTimeout(() => {
        this.$refs.departRef.loadDepartmentDetail()
      }, 0)
    },
    // 删除部门
    hDelete(id) {
      this.$confirm('确认删除吗', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          await deleteDepartment(id)
          console.log('删除成功')
          // 刷新列表
          this.loadDepartments()
        } catch (error) {
          console.log(error)
        }
      }).catch(() => {

      })
    },
    hSuccess() {
      this.loadDepartments()
      this.showDialog = false
    },
    hClose() {
      this.showDialog = false
    }
  }
}
</script>

<style></style>
